//
//  ZYLFindBackCollectionCell.m
//  Cartoon
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFindBackCollectionViewCell.h"
#import "Define.h"
#import "ZYLTopic_listsMixedModel.h"
#import "ZYLFindFrontSigleLayout.h"
#import "ZYLFindMidelColletionViewCell.h"
@interface ZYLFindBackCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, assign) BOOL slide;
@property (nonatomic, assign) CGFloat oldcontentOffset_x;


@end

@implementation ZYLFindBackCollectionViewCell

- (void)dealloc
{
    [_frontOneCollectionView release];
    [_frontTwoCollectionView release];
    [_frontThreeCollectionView release];
    [_frontFourCollectionView release];
    [_collectionviews release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self getData];
        [self createFrontCollectionView];
    }
    return self;
}

- (void)createFrontCollectionView
{
    self.slide = NO;
    self.oldcontentOffset_x = SCREEN_WIDTH;
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
   
    self.collectionviews = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.frame.size.height) collectionViewLayout:layout] autorelease];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, self.frame.size.height);
    layout.minimumLineSpacing = 0;
    self.collectionviews.backgroundColor = [UIColor greenColor];
    self.collectionviews.pagingEnabled = YES;
    self.collectionviews.bounces = NO;
    self.collectionviews.delegate = self;
    self.collectionviews.dataSource = self;
    [self.collectionviews registerClass:[ZYLFindMidelColletionViewCell class] forCellWithReuseIdentifier:@"collectionviews1"];
    [self.collectionviews registerClass:[ZYLFindMidelColletionViewCell class] forCellWithReuseIdentifier:@"collectionviews2"];
    [self.collectionviews registerClass:[ZYLFindMidelColletionViewCell class] forCellWithReuseIdentifier:@"collectionviews3"];
    [self.collectionviews registerClass:[ZYLFindMidelColletionViewCell class] forCellWithReuseIdentifier:@"collectionviews4"];
    
    [self.contentView addSubview:self.collectionviews];
    self.myScrollCount = 0;
    [self addObserver:self forKeyPath:@"myScrollCount" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        ZYLFindMidelColletionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionviews1" forIndexPath:indexPath];
        cell.model = self.topic_List[indexPath.item];
        return cell;
    } else if (indexPath.item == 1) {
        ZYLFindMidelColletionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionviews2" forIndexPath:indexPath];
        cell.model = self.topic_List[indexPath.item];
        return cell;
    } else if (indexPath.item == 2) {
        ZYLFindMidelColletionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionviews3" forIndexPath:indexPath];
        cell.model = self.topic_List[indexPath.item];
        return cell;
    } else if (indexPath.item == 3) {
        ZYLFindMidelColletionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionviews4" forIndexPath:indexPath];
        cell.model = self.topic_List[indexPath.item];
        return cell;
    }
    return nil;
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.slide = YES;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (!self.slide) {
        if ([keyPath isEqualToString:@"myScrollCount"] && [change objectForKey:@"new"] != [change objectForKey:@"old"]) {
            NSInteger new = [((NSNumber *)[change objectForKey:@"new"]) integerValue];
            [self.collectionviews setContentOffset:CGPointMake(self.collectionviews.frame.size.width * new, 0) animated:YES];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.slide == YES) {
        CGFloat x = scrollView.contentOffset.x;
        CGFloat x1 = x / (self.collectionviews.frame.size.width * 4);
        [self.moveViewDelegate changeMoveView:x1];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    CGFloat x1 = x / (self.collectionviews.frame.size.width * 4);
    
    if ((x + 1) > 0 && (x - 1) < SCREEN_HEIGHT) {
        [self.colorDelegate changeColor:x1 picNum:[self.topic_List[0] topics].count];
    } else if ((x + 1) > SCREEN_HEIGHT && (x - 1) < SCREEN_HEIGHT * 2) {
        [self.colorDelegate changeColor:x1 picNum:[self.topic_List[1] comics].count];
    } else if ((x + 1) > SCREEN_HEIGHT * 2 && (x - 1) < SCREEN_HEIGHT * 3) {
        [self.colorDelegate changeColor:x1 picNum:[self.topic_List[2] topics].count];
    } else if ((x + 1) > SCREEN_HEIGHT * 3) {
        [self.colorDelegate changeColor:x1 picNum:[self.topic_List[3] topics].count];
    }
    self.slide = NO;
}

//- (void)getData
//{
//    [ZYLTopic_listsMixedModel Topic_listsMixedWithblock:^(NSMutableArray *array) {
//        self.topic_List = array;
//        [self.collectionviews reloadData];
//        if (self.collectionviews == nil) {
//            [self createFrontCollectionView];
//        }else {
//            [self.collectionviews reloadData];
//        }
//    }];
//}
@end
